package Work_oj;

import java.util.HashMap;
import java.util.Map;

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
public class test138 {
    public Node copyRandomList(Node head) {
        Map<Node,Node> map = new HashMap<>();
        //先将两个链表的地址存储到map当中
        //Map(key,val)  get方法返回key对应的val
        Node cur = head;
        while (cur != null) {
            map.put(cur,new Node(cur.val));
            cur = cur.next;
        }
        //开始构建对应关系
        cur = head;
        while (cur != null) {
            Node node = map.get(cur);
            node.next = map.get(cur.next);
            node.random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }
}
